package com.yunlankeji.stemcells.fragemt;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.stemcells.activity.mine.LoginActivity;
import com.yunlankeji.stemcells.activity.mine.Mine_personal_space_searchActivity;
import com.yunlankeji.stemcells.activity.mine.mine_personal_space_followActivity;
import com.yunlankeji.stemcells.adapter.MyAdapter;
import com.yunlankeji.stemcells.fragemt.project.fragment_project_classify_all;
import com.yunlankeji.stemcells.fragemt.project.fragment_project_classify_artificial_intelligence;
import com.yunlankeji.stemcells.fragemt.project.fragment_project_classify_biotechnology;
import com.yunlankeji.stemcells.fragemt.project.fragment_project_classify_gene_detection;
import com.yunlankeji.stemcells.fragemt.project.fragment_project_classify_health_product;
import com.yunlankeji.stemcells.fragemt.project.fragment_project_classify_medical_cosmetology;
import com.yunlankeji.stemcells.fragemt.project.fragment_project_classify_medical_equipment;

import java.util.ArrayList;
import java.util.List;

public class fragment_project extends Fragment {
    private View contextView;// 总视图
    private TabLayout tabLayout;
    private ViewPager2 viewpager2;
    private ImageView imageView;
    private fragment_project context = null;
    private PopupWindow popupWindow;
    private int from = 0;

    List<String> titles=new ArrayList<>();
    List<Fragment> fragments=new ArrayList<>();

    ArrayList fragmentList = new ArrayList<Fragment>();


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contextView = inflater.inflate(R.layout.fragment_project, container, false);
        tabLayout = contextView.findViewById(R.id.tb_project_classify);
        viewpager2 = contextView.findViewById(R.id.vg_project_classify);
        imageView = contextView.findViewById(R.id.project_classify_iv);
        context = this;

        //添加标题
        titles.add("关注项目");
        titles.add("全部项目");
        titles.add("生物科技");
        titles.add("医疗器械");
        titles.add("基因检测");
        titles.add("医疗美容");
        titles.add("保健用品");
        titles.add("人工智能");

        //添加Fragment进去
        fragments.add(new fragment_mine_personal_space_follow());
        fragments.add(new fragment_project_classify_all());
        fragments.add(new fragment_project_classify_biotechnology());
        fragments.add(new fragment_project_classify_medical_equipment());
        fragments.add(new fragment_project_classify_gene_detection());
        fragments.add(new fragment_project_classify_medical_cosmetology());
        fragments.add(new fragment_project_classify_health_product());
        fragments.add(new fragment_project_classify_artificial_intelligence());

        //实例化适配器
        MyAdapter myAdapter = new MyAdapter(getFragmentManager(), getLifecycle(), fragments);
        //设置适配器
        viewpager2.setAdapter(myAdapter);
        //TabLayout和Viewpager2进行关联
        new TabLayoutMediator(tabLayout, viewpager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles.get(position));
            }
        }).attach();

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch(v.getId()){
                    case R.id.textView13:{
                        from = fragment_project.Location.RIGHT.ordinal();
                        break;
                    }

                }

                initPopupWindow();

            }
        });
        return contextView;
    }



    class popupDismissListener implements PopupWindow.OnDismissListener{

        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }

    }



    protected void initPopupWindow(){
        View popupWindowView = getLayoutInflater().inflate(R.layout.activit_project_pop, null);
        //内容，高度，宽度
        if(fragment_project.Location.BOTTOM.ordinal() == from){
            popupWindow = new PopupWindow(popupWindowView, WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        }else{
            popupWindow = new PopupWindow(popupWindowView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.FILL_PARENT, true);
        }
        //动画效果
        if(fragment_project.Location.RIGHT.ordinal() == from){
            popupWindow = new PopupWindow(popupWindowView, 600, WindowManager.LayoutParams.FILL_PARENT, true);
            popupWindow.setAnimationStyle(R.style.AnimationRightFade);

        }

        //菜单背景色
        ColorDrawable dw = new ColorDrawable(0xffffffff);
        popupWindow.setBackgroundDrawable(dw);
        //宽度
        //popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
        //高度
        //popupWindow.setHeight(LayoutParams.FILL_PARENT);
        //显示位置
        if(fragment_project.Location.RIGHT.ordinal() == from){
            popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.fragment_project, null), Gravity.RIGHT, 0, 0);
        }
        //设置背景半透明
        backgroundAlpha(0.5f);
        //关闭事件
        popupWindow.setOnDismissListener(new popupDismissListener());

        popupWindowView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
				/*if( popupWindow!=null && popupWindow.isShowing()){
					popupWindow.dismiss();
					popupWindow=null;
				}*/
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
                return false;
            }
        });


    }

    /**
     * 设置添加屏幕的背景透明度
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha)
    {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getActivity().getWindow().setAttributes(lp);
    }
    /**
     * 菜单弹出方向
     *
     */
    public enum Location {


        RIGHT,
        TOP,
        BOTTOM;

    }



}
